﻿/*
 * KLF Headless Browser
 * Written by Kris Oye <kristianoye@gmail.com>
 * Copyright (C) 2011-2012.  All rights reserved.
 */
namespace KLF.Lib.Browser.DOM.CSS
{
    #region Using Statements

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    #endregion

    /// <summary>
    /// The CSSRuleList interface provides the abstraction of an ordered 
    /// collection of CSS rules.
    /// 
    /// The items in the CSSRuleList are accessible via an integral index, 
    /// starting from 0.
    /// </summary>
    public class CSSRuleList
    {
        #region Attributes

        internal List<CSSRule> Rules { get; set; }

        #endregion

        #region Constructors

        private CSSRuleList()
        {
            Rules = new List<CSSRule>();
        }

        internal CSSRuleList(IEnumerable<CSSRule> rulesIn)
        {
            Rules = new List<CSSRule>(rulesIn);
        }

        #endregion

        #region DOM Level 2 CSS Compliance

        /// <summary>
        /// Add a rule to the collection.
        /// </summary>
        /// <param name="rule"></param>
        internal void Add(CSSRule rule)
        {
            Rules.Add(rule);
        }

        /// <summary>
        /// Inserts a rule at the specified index.
        /// </summary>
        /// <param name="index"></param>
        /// <param name="cSSRule"></param>
        internal void Insert(int index, CSSRule cSSRule)
        {
            Rules.Insert(index, cSSRule);
        }

        /// <summary>
        /// Used to retrieve a CSS rule by ordinal index. The order in this 
        /// collection represents the order of the rules in the CSS style 
        /// sheet. If index is greater than or equal to the number of rules 
        /// in the list, this returns null.
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public CSSRule item(int index)
        {
            if (index > -1 && index < Rules.Count)
                return Rules[index];
            else
                return null;
        }

        /// <summary>
        /// The number of CSSRules in the list. The range of valid child rule 
        /// indices is 0 to length-1 inclusive.
        /// </summary>
        public int length
        {
            get
            {
                return Rules.Count;
            }
        }

        /// <summary>
        /// Remove a rule from the collection.
        /// </summary>
        /// <param name="rule"></param>
        internal void Remove(CSSRule rule)
        {
            Rules.Remove(rule);
        }

        /// <summary>
        /// Removes a rule at the specified index.
        /// </summary>
        /// <param name="index"></param>
        internal void RemoveAt(int index)
        {
            Rules.RemoveAt(index);
        }

        #endregion
    }
}
